<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Beater extends Controller
{ 
    public function index(){
        $status = $this->request->param('status');
        $date = $this->request->param('date');
        $area = $this->request->param('area');
        $server = $this->request->param('server');
        $type = $this->request->param('type');
        $search_type = $this->request->param('search_type');
        $keyword = $this->request->param('keyword');
        $order_count = db('order')->alias('a');
        $order = db('order')->alias('a');
        $query = [];
        //时间
        if($date){
            $query['date'] = $date;
            $date = explode('-',$date);
            $order_count->where('a.create_time','between',[strtotime($date[0]),strtotime($date[1])]);
            $order->where('a.create_time','between',[strtotime($date[0]),strtotime($date[1])]);            
        }
        //区
        if($area){
            $order_count->where('a.area','like','%'.$area.'%');
            $order->where('a.area','like','%'.$area.'%');
            $query['area'] = $area;
        }
        //服
        if($server){
            $order_count->where('a.server','like','%'.$server.'%');
            $order->where('a.server','like','%'.$server.'%');
            $query['server'] = $server;
        }
        if($type){
            $order_count->where('a.type','like','%'.$type.'%');
            $order->where('a.type','like','%'.$type.'%');
            $query['type'] = $type;
        }
        //关键字
        if($search_type == 5 && $keyword){
            $order->join('tf_user u','a.mid = u.id','LEFT')->join('tf_user s','a.customer_id = s.id');
        }else if($search_type == 8 && $keyword){
            $order->join('tf_user u','a.mid = u.id')->join('tf_user s','a.customer_id = s.id','LEFT');
        }else{
            $order->join('tf_user u','a.mid = u.id','LEFT')->join('tf_user s','a.customer_id = s.id','LEFT');
        }
        if($keyword){
            if($search_type == 1){
                $order_count->where('a.role','like','%'.$keyword.'%');
                $order->where('a.role','like','%'.$keyword.'%');
            }else if($search_type == 2){
                $order_count->where('a.iphone','like','%'.$keyword.'%');
                $order->where('a.iphone','like','%'.$keyword.'%');
            }else if($search_type == 3){
                $order_count->where('a.taobao','like','%'.$keyword.'%');
                $order->where('a.taobao','like','%'.$keyword.'%');                
            }else if($search_type == 4){
                $order_count->where('a.login','like','%'.$keyword.'%');
                $order->where('a.login','like','%'.$keyword.'%');                
            }else if($search_type == 5){
                $order_count->join('tf_user u','a.customer_id = u.id')->where('u.name','like','%'.$keyword.'%');
                $order->where('s.name','like','%'.$keyword.'%');                
            }else if($search_type == 6){
                $order_count->where('a.number','like','%'.$keyword.'%');
                $order->where('a.number','like','%'.$keyword.'%');                  
            }else if($search_type == 7){
                $order_count->where('a.content','like','%'.$keyword.'%');
                $order->where('a.content','like','%'.$keyword.'%');                   
            }else if($search_type == 8){
                $order_count->join('tf_user u','a.mid = u.id')->where('u.name','like','%'.$keyword.'%');
                $order->where('u.name','like','%'.$keyword.'%');                  
            }
        }
        if(!isset($this->request->param()['status'])){
            $order_count->where('a.status','>',-1);
            $order->where('a.status','>',-1);            
        }else if($status == 'all'){
            $order_count->where('a.status','>',-1);
            $order->where('a.status','>',-1);
        }else if(intval($status) === 0){
            $order_count->where('a.status',0);
            $order->where('a.status',0);
        }else if($status){
            $order_count->where('a.status',$status);
            $order->where('a.status',$status);
        }else{
            $order_count->where('a.status','>',-1);
            $order->where('a.status','>',-1);
        }
        $count = $order_count->count();
        $data = $order->field('a.*,u.name as membername,s.name as servername')->where('mid',session('userinfo.id'))->order('id desc')->paginate(2,$count,['query'=>$query]);
        $this->assign('count',$count);
        $this->assign('data',$data);
        $this->assign('tags',db('tags')->select());
        //获取订单数量
        $tongji = Db::query("select  sum(if(status=0,1,0)) as sum0 ,sum(if(status=1,1,0)) as sum1,sum(if(status=2,1,0)) as sum2,sum(if(status=3,1,0)) as sum3,sum(if(status=4,1,0)) as sum4,sum(if(status=5,1,0)) as sum5,sum(if(status=6,1,0)) as sum6,sum(if(status=7,1,0)) as sum7,sum(if(status=8,1,0)) as sum8,sum(if(status=9,1,0)) as sum9,sum(if(status=10,1,0)) as sum10,sum(if(status=11,1,0)) as sum11 from tf_order");
        $this->assign('tongji',$tongji[0]);
        return $this->fetch();
    }

    public function getOrder(){
        $id = $this->request->post('id');
        if (Db('order')->where('id',$id)->update(['status'=>1])) {
            $data['order_id'] = $id;
            $data['msg'] = '订单已在处理中，代练员'.session('userinfo.name').',期间请不要顶号，有任何问题可以与客服联系。';
            $data['name'] = session('userinfo.name');
            $data['create_time'] = time();
            Db('order_search')->insert($data);
            return ['code'=>1,'msg'=>'订单接收成功'];
        }else{
            return ['code'=>0,'msg'=>'订单接收失败'];
        }
    }


    public function overOrder(){
        $id = $this->request->post('id');
        if (Db('order')->where('id',$id)->update(['status'=>2])) {
            $data['order_id'] = $id;
            $data['msg'] = '订单代练结束，正在验收中，有任何问题可以与客服联系。';
            $data['name'] = session('userinfo.name');
            $data['create_time'] = time();
            Db('order_search')->insert($data);
            return ['code'=>1,'msg'=>'订单更新成功'];
        }else{
            return ['code'=>0,'msg'=>'订单新失败'];
        }
    }

    public function mygroup(){
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $res = db('group_info')->update($data);
            if($res!==false){
                return ['code'=>1,'msg'=>'更新成功'];
            }else{
                return ['code'=>0,'msg'=>'更新失败'];
            }
        }else{
            $datalist = db('group_info')->alias('f')->join('tf_group g','g.id = f.group_id')->field('f.*,g.name,g.start_time,g.status,g.number')->where('f.uid',session('userinfo.id'))->paginate(20);
            $this->assign('datalist',$datalist);
            return $this->fetch();
        }
    }
}
